import pandas as pd
def winddata2df(r,columns=None):
    if r.Data:
        if len(r.Data[0])!=len(r.Times):
            if columns:
                a= pd.DataFrame(data=r.Data, index=columns).T
            else:
                a= pd.DataFrame(data=r.Data,index=r.Fields).T
        else:
            if columns:
                a= pd.DataFrame(data=r.Data, index=columns, columns=r.Times).T
            else:
                a= pd.DataFrame(data=r.Data,index=r.Fields,columns=r.Times).T
            a["timekey"] = pd.to_datetime(a.index)
            a.sort_index(inplace=True)
    else:
        return pd.DataFrame()
    return a
def winddata2dict(r,key,columns=None):
    a = winddata2df(r, columns=columns)
    a.index=a[key]
    return a.T.dict()
def winddata2list(r,columns=None):
    data=winddata2df(r,columns=columns)
    rst=[]
    for i in data.to_dict("records"):
        if "timekey" in i.keys():
            i["timekey"]=i["timekey"].to_pydatetime()
        rst.append(i)
    return rst